﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using wzSqlAdmin.nwDataBase;
using System.Collections;
using wzSqlAdmin.Util;
using System.Windows.Data;
using System.Windows.Markup;
using System.Collections.ObjectModel;


namespace wzSqlAdmin.pagine
{
    public partial class Risultati : FloatableWindow
    {
        private nwDataBase.InwDataBaseClient db;
        public Dictionary<string, string> parametri = new Dictionary<string, string>();
        public string Table;
        private ProgressBar pb = new ProgressBar();
        // List of tables returned from Web Services
        ObservableCollection<nwDataBase.DataTableInfo> _tables;
        public Risultati()
        {
            InitializeComponent();
            string host = Application.Current.Host.Source.AbsoluteUri;
            host = host.Substring(0, host.LastIndexOf("/"));
            host = host.Substring(0, host.LastIndexOf("/"));
            db = new nwDataBase.InwDataBaseClient("BasicHttpBinding_InwDataBase", host + "/nwDataBase.svc");
            db.getSqlCompleted += new EventHandler<getSqlCompletedEventArgs>(RecDati);
            pb.Width = 300;
            pb.Height = 50;
            pb.Visibility = Visibility.Collapsed;
            LayoutRoot.Children.Add(pb);
        }

        

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void PageChanged(object sender, EventArgs e)
        {

        }

        private void PageChanging(object sender, System.ComponentModel.CancelEventArgs e)
        {

        }

        private void SizeChange(object sender, SizeChangedEventArgs e)
        {

        }

        private void btnPage(object sender, RoutedEventArgs e)
        {
            Button b = (Button)sender;
            string c = ((string)b.CommandParameter).ToLower();
            int page = (int)cboAttPage.SelectedValue;
            

            if (c == "next")
            {
                if (cboAttPage.SelectedIndex != cboAttPage.Items.Count - 1)
                {
                    page = (int)cboAttPage.SelectedValue + 1;
                    db.getSqlAsync(parametri, "select * from " + Table, page, (int)cboPage.SelectedValue);
                    Busy.IsBusy = true;
                }

            }
            if (c == "first")
            {
                page = 1;
                db.getSqlAsync(parametri, "select * from " + Table, page, (int)cboPage.SelectedValue);
                Busy.IsBusy = true;
            }
            if (c == "prev")
            {
                if (cboAttPage.SelectedIndex != 0)
                {
                    page = (int)cboAttPage.SelectedValue - 1;
                    db.getSqlAsync(parametri, "select * from " + Table, page, (int)cboPage.SelectedValue);
                    cboAttPage.SelectedValue = (int)cboAttPage.SelectedValue + 1;
                    Busy.IsBusy = true;
                }
            }
            if (c == "last")
            {
                page = (int)lbTot.Content;
                db.getSqlAsync(parametri, "select * from " + Table, page, (int)cboPage.SelectedValue);
                Busy.IsBusy = true;
            }
            cboAttPage.SelectedValue = page;
            
            
        }
        private void RecDati(object sender, getSqlCompletedEventArgs e)
        {
             

            if (e.Error == null)
            {
                if (e.ServiceError == null)
                {
                    // Table Data : Table 0 in array
                    _tables = e.Result.Tables;
                    IEnumerable list = DynamicDataBuilder.GetDataList(e.Result, 0);
                    ris.ItemsSource = list;
                    lbTot.Content = ((e.TotRecors / (int)cboPage.SelectedValue) + 1);
                    if (cboAttPage.Items.Count != ((e.TotRecors / (int)cboPage.SelectedValue) + 1))
                    {
                        cboAttPage.Items.Clear();
                        for (int i = 1; i <= ((e.TotRecors / (int)cboPage.SelectedValue) + 1); i++)
                        {
                            cboAttPage.Items.Add(i);
                        }
                    }
                    if (cboAttPage.SelectedIndex == -1)
                        cboAttPage.SelectedIndex = 0;
                }
                else
                {
                    MessageBox.Show(e.ServiceError.Message);
                }

            }
            else
            {
                MessageBox.Show(e.Error.Message);
            }
            Busy.IsBusy = false;


        }

        private void cambioPage(object sender, SelectionChangedEventArgs e)
        {
            if (cboAttPage.SelectedValue != null)
            {
                int page = (int)cboAttPage.SelectedValue;

                db.getSqlAsync(parametri, "select * from " + Table, page, (int)cboPage.SelectedValue);
                ris.ItemsSource = null;
                Busy.IsBusy = true;
            }
        }
        private void cambioPageSize(object sender, SelectionChangedEventArgs e)
        {
            int page = 1;

            db.getSqlAsync(parametri, "select * from " + Table, page, (int)cboPage.SelectedValue);
            ris.ItemsSource = null;
            Busy.IsBusy = true;

        }
    }
}

